package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Book;
import com.itheima.domain.Record;
import com.itheima.domain.User;
import com.itheima.entity.PageResult;
import com.itheima.mapper.BookMapper;
import com.itheima.service.BookService;
import com.itheima.service.RecordService;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private RecordService recordService;

    @Override
    public PageResult selectNewBooks(int pageNum, int pageSize) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        Page<Book> page = bookMapper.selectBooks();
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Book findById(String id) {
        return bookMapper.findById(id);
    }

    @Override
    public Integer borrowBook(Book book) {
        //设置借阅状态（0可借阅，1借阅中,2归还中,3已下架）
        book.setStatus("1");
        //借阅时间
        //设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String borrowTime = format.format(new Date());
        book.setBorrowTime(borrowTime);
        //调用mapper
        return bookMapper.borrowBook(book);
    }

    @Override
    public PageResult search(Book book, int pageNum, int pageSize) {
        //分页开启查询
        PageHelper.startPage(pageNum,pageSize);
        //调用Mapper
        Page<Book> page = bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer addBook(Book book) {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
        //设置上架时间格式
        String uploadTime = dataFormat.format(new Date());
        book.setUploadTime(uploadTime);
        return bookMapper.addBook(book);
    }

    @Override
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchBorrowed(Book book, User user, int pageNum, int pageSize) {
        //分页开启查询
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page =null;
        //将当前用户信息传入到查询条件中
        book.setBorrower(user.getName());
        //如果是管理员，查询当前用户借阅但未归还的图书和所有待确认归还的图书
        //区分不同用户
        if("ADMIN".equals(user.getRole())){
            page = bookMapper.selectBorrowed(book);
        }else {
            //查询当前用户未归还的图书信息
            page = bookMapper.selectMyBorrowed(book);
        }
        //返回分页结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public boolean returnBook(String id, User user) {
        //根据id查询图书信息
        Book book = this.findById(id);
        //判断是否是当前用户借阅的图书是否同一个人
        boolean rb = book.getBorrower().equals(user.getName());
        //如果当前用户是借阅人，则可以归还图书
        if (rb){
            //设置归还状态（0可借阅，1借阅中,2归还中,3已下架）
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return rb;
    }

    //归还确认
    @Override
    public boolean returnConfirm(String id) {
        //根据id查询图书信息
        Book book = bookMapper.findById(id);
        String borrower = book.getBorrower();
        String borrowTime = book.getBorrowTime();
        //设置归还状态（0可借阅，1借阅中,2归还中,3已下架）
        book.setStatus("0");
        //清空借阅人和借阅时间
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        //调用Mapper
        Integer num = bookMapper.editBook(book);
        if (num>0){
            //确认归还无误，我们新增借阅信息
            Record record = new Record();
            //设置记录信息
            record.setBookisbn(book.getIsbn());
            record.setBookname(book.getName());
            record.setBorrowTime(borrowTime);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String remandTime = simpleDateFormat.format(new Date());
            record.setRemandTime(remandTime);
            record.setBorrower(borrower);


            recordService.addRecord(record);

        }


        return num>0;
    }
}
